package pri.ydy.staservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import pri.ydy.commonUtils.R;
import pri.ydy.staservice.client.UcenterClient;
import pri.ydy.staservice.entity.StatisticsDaily;
import pri.ydy.staservice.mapper.StatisticsDailyMapper;
import pri.ydy.staservice.service.StatisticsDailyService;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 网站统计日数据 服务实现类
 * </p>
 *
 * @author YeDongyong
 * @since 2021-03-01
 */
@Service
public class StatisticsDailyServiceImpl extends ServiceImpl<StatisticsDailyMapper, StatisticsDaily> implements StatisticsDailyService {

    @Autowired
    private UcenterClient ucenterClient;

    @Override
    public void registerCount(String day) {

        //添加记录之前先删除相同日期的记录
        QueryWrapper<StatisticsDaily> wrapper=new QueryWrapper<>();
        wrapper.eq("date_calculated",day);
        baseMapper.delete(wrapper);


        //1 远程调用获得某一天的注册人数
        R registerR=ucenterClient.countRegister(day);
        Integer countRegister=(Integer) registerR.getData().get("countRegister");

        //把数据添加到数据表中
        Integer loginNum = RandomUtils.nextInt(100, 200);//TODO 登录数获取
        Integer videoViewNum = RandomUtils.nextInt(100, 200);//TODO 视频观看数获取
        Integer courseNum = RandomUtils.nextInt(100, 200);//TODO 新增课程数获取

        StatisticsDaily sta=new StatisticsDaily();
        sta.setCourseNum(courseNum);
        sta.setDateCalculated(day); //统计日期添加
        sta.setLoginNum(loginNum);
        sta.setVideoViewNum(videoViewNum);
        sta.setRegisterNum(countRegister);
        baseMapper.insert(sta);

    }

    //图表显示,返回两部分数据
    @Override
    public Map<String, Object> getShowData(String type, String begin, String end) {
        //根据条件查询数据
        QueryWrapper<StatisticsDaily> wrapper=new QueryWrapper<>();
        wrapper.between("date_calculated",begin,end);
        wrapper.select("date_calculated",type);//获得两部分数据
        List<StatisticsDaily> staList = baseMapper.selectList(wrapper);
        //前端要求数据json格式(由list集合可得),还有就是对象格式
        List<String> date_calculated=new ArrayList<>();
        List<Integer> num=new ArrayList<>();
        //分别向两个list集合中放数据
        for (StatisticsDaily daily : staList) {
            //向第一个x轴放入
            date_calculated.add(daily.getDateCalculated());
            //向第二个y轴放入数据,比较放入
            switch (type) {
                case "login_num":
                    num.add(daily.getLoginNum());
                    break;
                case "register_num":
                    num.add(daily.getRegisterNum());
                    break;
                case "video_view_num":
                    num.add(daily.getVideoViewNum());
                    break;
                case "course_num":
                    num.add(daily.getCourseNum());
                    break;
                default:
                    break;
            }
        }
        //将两个list放入map封装
        Map<String ,Object> map=new HashMap<>();
        map.put("date_calculated",date_calculated);
        map.put("numDataList",num);

        return map;
    }
}
